package com.cxyd.modules.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.DateUtils;
import com.cxyd.modules.log.dao.SysLogWorkDao;
import com.cxyd.modules.log.dto.SysLogWorkDTO;
import com.cxyd.modules.log.entity.SysLogWorkEntity;
import com.cxyd.modules.log.service.SysLogWorkService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 作业日志
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-11-14
 */
@Service
public class SysSysLogWorkServiceImpl extends CrudServiceImpl<SysLogWorkDao, SysLogWorkEntity, SysLogWorkDTO> implements SysLogWorkService {
    @Override
    public PageData<SysLogWorkDTO> page(Map<String, Object> params) {
        IPage<SysLogWorkEntity> page = baseDao.selectPage(
                getPage(params, "create_date", false),
                getWrapper(params)
        );
        return getPageData(page, SysLogWorkDTO.class);
    }

    @Override
    public QueryWrapper<SysLogWorkEntity> getWrapper(Map<String, Object> params) {
        String workName = (String) params.get("workName");
        String workMsg = (String) params.get("workMsg");
        String workProc = (String) params.get("workProc");
        String contNo = (String) params.get("contNo");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<SysLogWorkEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(workProc), "work_proc", workProc);
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.like(StringUtils.isNotBlank(workName), "work_name", workName);
        wrapper.like(StringUtils.isNotBlank(workMsg), "work_msg", workMsg);
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            Date start = DateUtils.parse(startDate, DateUtils.DATE_TIME_PATTERN);
            Date end = DateUtils.parse(endDate, DateUtils.DATE_TIME_PATTERN);
            wrapper.between("create_date", start, end);
        }

        return wrapper;
    }

    @Override
    public void workLogging(String workName, String contNo, String code, String msg, String writeItem, String workProc, String userName, long times) {
        SysLogWorkEntity logEntity = new SysLogWorkEntity();
        logEntity.setWorkName(workName);
        logEntity.setContNo(contNo);
        logEntity.setWorkProc(workProc);
        logEntity.setWorkCode(code);
        logEntity.setWorkMsg(msg);
        logEntity.setWriteItem(writeItem);
        logEntity.setWorkSec(times);
        logEntity.setCreatorName(userName);
        logEntity.setCreateDate(new Date());
        baseDao.insert(logEntity);
    }
}